package com.atxiao.demo.controller;

import com.atxiao.demo.bean.Flow;
import com.atxiao.demo.bean.FlowLog;
import com.atxiao.demo.bean.User;
import com.atxiao.demo.search.FlowLogSearch;
import com.atxiao.demo.service.FlowLogServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

/**
 * @author biHuGang
 * @date 2021/3/15
 */
@Controller
public class FlowLogController {
    //收入
    //double income = 0;
    //支出
    //double outlay = 0;
    //流水金额
    double price = 0;
    //交付时间
    Date time;
    //流水记录
    List<Flow> flows = new ArrayList<>();
    @Autowired
    private FlowLogServiceImpl flowLogService;

    /**
     * 修改人：杨磊
     * 问题1：按照单一卡号进行查询，可以改为idcard
     * 问题2：传参中使用基础数据类型可能会出现空指针，导致数据错误
     * @param card
     * @param session
     * @return
     */
    @RequestMapping("/select/Flow")
    @ResponseBody
    public List<Flow> selectFlow(Integer card, HttpSession session) {
        //根据session中的用户名取出用户名
        String username = (String) session.getAttribute("username");
        //流水金额,交易时间,存取标志
        List<FlowLog> flowLogs = flowLogService.selectFlowLog(card);
        //1表示支出,2表示收入,3表示转账
        for (FlowLog flowLog : flowLogs) {
            price = flowLog.getPrice();
            time = flowLog.getBusinessTime();
            if ("1".equals(flowLog.getBusinessType())) {
                flows.add(new Flow(username, price, time, 0, price));
                //outlay += flowLog.getPrice();
            } else if ("2".equals(flowLog.getBusinessType())){
                flows.add(new Flow(username, price, time, price, 0));
                //income += flowLog.getPrice();
            }else {
                flows.add(new Flow(username, price, time, price, price));
            }
            //price += flowLog.getPrice();
            //time = flowLog.getBusinessTime();
        }
        return flows;
        //return new Flow(username, price, time, income, outlay);
    }


    //去明细界面
//    @RequestMapping("/detail")
//    public String toDetail() {
//        return "detail";
//    }

    @RequestMapping("/detail")
    public String selectFlow02(@ModelAttribute("search") FlowLogSearch search, HttpSession session, Model model) {
        User user = (User)session.getAttribute("user");

        System.out.println(search.toString());
        List<FlowLog> flowLogs = flowLogService.selectFlowLogBySearch(search, user.getUserIdCard());
        flowLogs.forEach(flowLog -> {
            flowLog.setBusinessTime(getDaDate(flowLog.getBusinessTime()));
        });
//        flowLogs.forEach(flowLog -> flowLog.setBusinessTime(flowLog.getBusinessTime().));
        model.addAttribute("flowLogs",flowLogs);
        List<String> types = flowLogService.selectTypes();
        model.addAttribute("types",types);
        return "detail";
    }


    private Date getDaDate(Date date){
        date.setTime(date.getTime() - 8*60*60*1000);
       String dateS = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS").format(date);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
        try {
            date = sdf.parse(dateS);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;

    }
}
